package com.lgj.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String driverClass;
    private static String url;
    private static String user;
    private static String password;
    private static Connection connection;


//    static{
//        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("com/lgj/config.properties");
//        Properties prop = new Properties();
//        try {
//            prop.load(is);
//            driverClass = prop.getProperty("driverClass");
//            url = prop.getProperty("url");
//            user = prop.getProperty("user");
//            password = prop.getProperty("password");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


    //获取数据库连接
    public static Statement connection(){



        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("com/lgj/config.properties");
        Properties prop = new Properties();
        try {
            prop.load(is);
            driverClass = prop.getProperty("driverClass");
            url = prop.getProperty("url");
            user = prop.getProperty("user");
            password = prop.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }



        Statement statement = null;
        try {
            System.out.println(driverClass);
            Class.forName(driverClass);
//            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println(url);
            System.out.println(user);
            System.out.println(password);
            connection = DriverManager.getConnection(url, user, password);

//            connection = DriverManager.getConnection("jdbc:mysql://192.168.2.115:3306/javaWeb", "root", "123456@lgj123.");
            statement = connection.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return statement;
    }


    //关闭数据库，执行者连接
    public static void close(Connection connection, Statement statement){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Statement state = JDBCUtils.connection();
        System.out.println("1111");
    }
}
